import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jicaimao_flutter/utils/layout_type.dart';
import 'package:jicaimao_flutter/utils/sp_key.dart';
import 'package:jicaimao_flutter/utils/sp_utils.dart';
import 'package:jicaimao_flutter/views/home_page.dart';
import 'package:jicaimao_flutter/views/mine/mine_page.dart';
import 'package:jicaimao_flutter/views/produt/product_page.dart';
import 'package:jicaimao_flutter/views/splash/splashPage.dart';

///主页面 测试提交
class TabMainPage extends StatefulWidget {
  @override
  TabPage createState() => TabPage();
}

class TabPage extends State<TabMainPage> with SingleTickerProviderStateMixin {
  LayoutType _layoutSelection = LayoutType.home;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  void _onLayoutSelected(LayoutType selection) {
    setState(() {
      _layoutSelection = selection;
    });
  }

  void _onSelectTab(int index) {
    switch (index) {
      case 0:
        _onLayoutSelected(LayoutType.home);
        break;
      case 1:
        _onLayoutSelected(LayoutType.product);
        break;
      case 2:
        _onLayoutSelected(LayoutType.mine);
        break;
    }
  }

  Color _colorTabMatching({LayoutType layoutSelection}) {
    return _layoutSelection == layoutSelection ? Colors.purple : Colors.grey;
  }

  BottomNavigationBarItem _buildItem(
      {IconData icon, LayoutType layoutSelection}) {
    String text = layoutName(layoutSelection);
    return BottomNavigationBarItem(
        icon: Icon(
          icon,
          color: _colorTabMatching(layoutSelection: layoutSelection),
        ),
        title: Text(
          text,
          style: TextStyle(
              color: _colorTabMatching(layoutSelection: layoutSelection)),
        ));
  }

  Widget _buildBody() {
    switch (_layoutSelection) {
      case LayoutType.home:
        return HomePage();
      case LayoutType.product:
        return ProductPage();
      case LayoutType.mine:
        return MinePage();
    }
    return null;
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      items: <BottomNavigationBarItem>[
        _buildItem(icon: Icons.home, layoutSelection: LayoutType.home),
        _buildItem(icon: Icons.email, layoutSelection: LayoutType.product),
        _buildItem(
            icon: Icons.account_circle, layoutSelection: LayoutType.mine),
      ],
      onTap: _onSelectTab,
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
      child: Scaffold(
        body: _buildBody(),
        bottomNavigationBar: _buildBottomNavigationBar(),
      ),
      onWillPop: _exitApp,
    );
  }


  Future<bool> _exitApp() async {
   await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }
}
